<?php

namespace App\Http\Controllers;

use App\Http\Requests\ImRequest;
use App\Models\FsUserIm;
use App\Models\User;
use App\Models\UserIm;
use App\Transformers\ImTransformer;
use Illuminate\Http\Request;

class ImController extends Controller
{
    /**
     * @api {post} /api/im 即时通讯添加
     * @apiGroup Im
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  用户id
     * @apiParam {string} im  枚举qq或者wx
     * @apiParam {string} value  值
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     */
    public function store(ImRequest $request, UserIm $im){
        $data = $request->all();
        $data['user_id'] = $data['id'];
        $im->create($data);

        return $this->response->created();
    }

    /**
     * @api {patch} /api/im/:id 即时通讯修改
     * @apiGroup Im
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} url  即时通讯id（必传参数）
     * @apiParam {string} im  枚举qq或者wx
     * @apiParam {string} value  值
     * @apiParam {string} status  1启用0未启用
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "message": "修改成功"
     *       }
     */
    public function update(ImRequest $request, UserIm $im){
        $im->update($request->all());

        return response()->json(['message' => '修改成功']);
    }
    /**
     * @api {get} /api/im 即时通讯列表
     * @apiGroup Im
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "im": "qq",
     *                   "user_name": "admin",
     *                   "user_id": 1,
     *                   "value": "645799612",
     *                   "status": 1,
     *                   "created_at": "",
     *                   "updated_at": "2019-01-02 15:44:54"
     *               },
     *            ]
     *        }
     */
    public function index(UserIm $im){
        $data = $im->all();

        return $this->response->collection($data, new ImTransformer());
    }

    /**
     * @api {delete} /api/im 即时通讯删除
     * @apiGroup Im
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} url  即时通讯id（必传参数）
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 204 OK
     */
    public function destroy(UserIm $im){
        $im->delete();

        return $this->response->noContent();
    }

    /**
     * @api {get} /api/im 即时通讯展示
     * @apiGroup Im
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} url  即时通讯id（必传参数）
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 204 OK
     */
    public function show(UserIm $im){
        return $this->response->item($im, new ImTransformer());
    }

    /**
     * @api {delete} /api/bind/fsUser/im/index?fs_user_id=51&page=1&limit=2&include=user 绑定会员可用微信列表
     * @apiGroup Im
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  会员id传了单独绑定，不传批量
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *
     *       {
     *           "data": [
     *               {
     *                   "id": 2,
     *                   "im": "wx",
     *                   "alias": "",
     *                   "user_id": 1,
     *                   "value": "11111222222222",
     *                   "status": 1,
     *                   "created_at": "2019-01-02 15:29:35",
     *                   "updated_at": "2019-01-08 15:51:01",
     *                   "user": {
     *                       "id": 1,
     *                       "name": "admin",
     *                       "section": "",
     *                       "branch": "",
     *                       "role": "",
     *                       "section_id": null,
     *                       "branch_id": null,
     *                       "role_id": null,
     *                       "status": 1,
     *                       "created_at": "",
     *                       "updated_at": "2019-01-18 14:38:13"
     *                   }
     *               }
     *           ],
     *               "meta": {
     *                       "pagination": {
     *                       "total": 4,
     *                       "count": 1,
     *                       "per_page": 1,
     *                       "current_page": 1,
     *                       "total_pages": 4,
     *                       "links": {
     *                       "previous": null,
     *                       "next": "http://dianyun2.com/api/bind/fsUser/im/index?page=2"
     *                   }
     *               }
     *           }
     *       }
     */
    public function bindFsUserIndex(Request $request,FsUserIm $fsUserIm){
        $data = $request->validate([
            'fs_user_id'=>'nullable|exists:mysql_branch.fs_user,id',
        ]);
        if(isset($data['fs_user_id'])){
            $res = $fsUserIm->where('fs_user_id',$data['fs_user_id'])->pluck('im_id')->toArray();
            $builder = UserIm::query()->where('status', true);
            if($res){
                $builder->whereNotIn('id',$res);
            }
            $result = $builder->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        }else{
            $result = UserIm::where('status', true)->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        }
        return $this->response->paginator($result, new ImTransformer());

    }
}
